package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.MompOrgInfoMapper;
import cc.rengu.igas.momp.common.dao.impl.MompOrgInfoMapperImpl;
import cc.rengu.igas.momp.common.entity.TBOrgInfo;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.TbOrgInfoBean;
import cc.rengu.igas.momp.facade.request.QueryOrgListRequest;
import cc.rengu.igas.momp.facade.response.QueryOrgListResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销活动商户/用户列表查询功能实现
 *
 * @author liujinan
 * @date 2021/3/22 17:26
 */
public class QueryOrgListService extends RadpService {


    /**
     * 法人机构号
     */
    private String globalInstId;
    /**
     * 操作类型
     */
    private String globalQueryType;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 操作角色
     */
    private String globalQueryRole;
    /**
     * 操作人所属机构
     */
    private String globalQueryOrgId;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;
    /**
     * 机构编号
     */
    private String globalOrgId;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryOrgListRequest queryOrgListRequest = new QueryOrgListRequest();
            ConvertUtil.convertOutput(queryOrgListRequest);

            QueryOrgListResponse queryOrgListResponse = new QueryOrgListResponse();
            queryOrgListResponse.setHeader(queryOrgListRequest.getHeader());
            BizResponse<QueryOrgListResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryOrgListResponse);

            /* 报文检查 */
            messageValidation(queryOrgListRequest);

            rglog.info("机构<{}>在<{}>由<{}>开始查询营销活动商戶/用戶列表信息流程", globalInstId, globalTimeStamp, globalQueryId);

            // 服务调用
            BizResponse<QueryOrgListResponse> bizResponseNew = queryOrgList(xmlTreeUtil, queryOrgListRequest, queryOrgListResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询营销活动商戶、用戶列表信息
     *
     * @param xmlTreeUtil          内部XML树
     * @param queryOrgListRequest  营销活动商戶、用戶列表查询接口请求对象
     * @param queryOrgListResponse 营销活动商戶、用戶d列表查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryMcMerUserListInfoResponse> 营销活动商戶、用戶列表查询接口应答对象
     * @throws BizException 业务异常信息
     * @author Jinan Liu
     * @date 2020/5/27 22:41
     */
    private BizResponse<QueryOrgListResponse> queryOrgList(XmlTreeUtil xmlTreeUtil, QueryOrgListRequest queryOrgListRequest, QueryOrgListResponse queryOrgListResponse)
            throws Exception {

        BizResponse<QueryOrgListResponse> bizResponse = new BizResponse<>();

        List<TbOrgInfoBean> orgInfoList = new ArrayList<>();

        List<TBOrgInfo> tbOrgInfoList = selectMompOrgInfoListBySuperOrgId();
        if (tbOrgInfoList != null) {
            orgInfoList = tbOrgInfoList.stream().map(this::convertOrgInfo).collect(Collectors.toList());
        }

        queryOrgListResponse.setOrgInfoList(orgInfoList);
        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryOrgListResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryOrgListResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());


        bizResponse.setResult(queryOrgListResponse);

        return bizResponse;
    }

    private TbOrgInfoBean convertOrgInfo(TBOrgInfo orgInfo) {
        TbOrgInfoBean tbOrgInfoBean = new TbOrgInfoBean();
        try {
            tbOrgInfoBean.setOrgId(orgInfo.getOrgId());
            tbOrgInfoBean.setOrgName(orgInfo.getOrgName());
            tbOrgInfoBean.setOrgHierarchy(orgInfo.getOrgHierarchy());
            tbOrgInfoBean.setSuperOrgId(orgInfo.getSuperOrgId());
        } catch (Exception e) {
            rglog.error("转换营销活动临时表信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return tbOrgInfoBean;
    }

    /**
     * 请求报文体内容检查
     *
     * @param queryOrgListRequest 机构列表查询接口
     * @author Jinan Liu
     * @date 2020/6/20 09:55
     */
    private void messageValidation(QueryOrgListRequest queryOrgListRequest) {

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryOrgListRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = queryOrgListRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = queryOrgListRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = queryOrgListRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(queryOrgListRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = queryOrgListRequest.getQueryOrgId();
        }

        globalOrgId = queryOrgListRequest.getOrgId();
        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }

    private List<TBOrgInfo> selectMompOrgInfoListBySuperOrgId() throws Exception {
        MompOrgInfoMapper orgInfoMapper = new MompOrgInfoMapperImpl();
        return orgInfoMapper.selectMompOrgInfoListBySuperOrgId(globalInstId, globalOrgId);
    }
}
